# coding=utf-8
"""
作者：董新强 
创建时间：
描述：
"""
from datetime import datetime

from framework.utilities import to_int


class ServiceBase:
    def __init__(self, usr):
        self.usr = usr
        self.uid = usr['id'] if usr else 0
        self.now = datetime.now()

    def ufield(self, instance, fname, dic, field_type=str):
        """
        更新实例字段
        :param instance: 实例
        :param fname: 字段名称
        :param reqs: 字典
        :param field_type:字典类型
        :return:
        """
        if field_type is int:
            setattr(instance, fname, to_int(dic.get(fname), 0))
            return

        setattr(instance, fname, dic.get(fname) or getattr(instance, fname))
